'''
Created on 21 sep. 2010

@author: christianp
'''
'''
import wx
import threading
from wxPython.wx import *
from BruTile.BruTile import BruTile
'''
from XML.XMLHandler import XMLHandler
from BruTile.Utilities import Utilities
from BruTile.TileSchema import TileSchema
from TileSeeder.LayerHandler import LayerHandler

ID_TEXTCTRL = 10000
ID_BUTTON = 10001
ID_LISTBOX = 10002
ID_FOREIGN = 10003
ID_CHECKBOX = 10004
ID_SLIDER = 10005
ID_GAUGE = 10006
ID_TEXT = 10007

class GUIHandler(object):
        
    #map = None;
    txtTCUrl = None
    lboxTC = None
    
    #tempSeeds = []
    
    width = 0
    height = 0
    
    def __init__(self):
        '''GUIHandler'''
    
    def DeselectLayer(self, name):
        for i in range(0, len(self.lboxTC.Items)):
            if (self.lboxTC.IsSelected(i)):
                item = self.lboxTC.Items[i]
                if (item == name):
                    self.lboxTC.Deselect(i)
        
    def seedIsSelected(self):
        for i in range(0, len(self.lboxTC.Items)):
            if (self.lboxTC.IsSelected(i)):
                return True
        return False
    
    def onLoadHandler(self, pscreenOpen):
        #self.layer_handler = LayerHandler()
        #self.__lbox_is_filled = self.layer_handler.InitTileSeeds(self.txtTCUrl.GetValue(), self.lboxTC, pscreenOpen)
        
        xmlHandler = XMLHandler()
        xmlHandler.load_capabilities(self.txtTCUrl.GetValue())
        self.lboxTC.Clear()
        has_items = False
        for attributes in xmlHandler.capabilities_handler.attributes:
            tile_schema = TileSchema()
            tile_schema.src = str(attributes.get("srs",""))
            tile_schema.name = str(attributes.get("title",""))
            self.lboxTC.Append(tile_schema.name, tile_schema)
            has_items = True
        return 
        
        self.__lbox_is_filled = self.lboxTC.Items > 0
        print("xc")
    
    def onApplyHandler(self):
        if (self.__lbox_is_filled and self.labelsAreFilled()):        
                extent = self.getSelectedLayerExtent()
                tile_seed = self.lboxTC.GetClientData(self.lboxTC.GetSelection())[1]
                if(extent.isIntersecting(tile_seed.tile_schema.extent)):
                    extent = extent.intersectsWith(tile_seed.tile_schema.extent)

                self.tile_seeder.proccessTileSeed(self.txtTCUrl.GetValue()+tile_seed.tile_schema.name, tile_seed, extent, self.gaugeTiles, self.stxtGaugeDone, self.stxtGaugeRemaining, self.labelTilesToBeDone, self.labelTilesFailed)
    
    def getSelectedLayerExtent(self):
        extent = self.lboxTC.GetClientData(self.lboxTC.GetSelection())[1].tile_schema.extent
        temp_extent = None
        try:
            temp_extent = Utilities.getExtent(float(self.labelMinX.GetValue()), 
                                                        float(self.labelMinY.GetValue()), 
                                                        float(self.labelMaxX.GetValue()), 
                                                        float(self.labelMaxY.GetValue()))
        except ValueError:
            print("Only numeric values are allowed ")
            #extent = self.lboxTC.GetClientData(self.lboxTC.GetSelection()).tile_schema.extent
        
        tile_seed = self.lboxTC.GetClientData(self.lboxTC.GetSelection())[1]
        if(extent.isIntersecting(tile_seed.tile_schema.extent)):
            extent = temp_extent.intersectsWith(tile_seed.tile_schema.extent)
        return extent

    def labelsAreFilled(self):
        if not (self.labelMinX.IsEmpty()):
            if not (self.labelMinY.IsEmpty()):
                if not (self.labelMaxX.IsEmpty()):
                    if not (self.labelMaxY.IsEmpty()):
                        return True
    
    def clearLabels(self):
        self.labelTilesToBeDone.Clear()
        self.lboxZoomLevels.Clear()
    
    def setSelectedZoomLvl(self, tile_seed):
        for resolution in tile_seed.tile_schema.resolutions:
            self.lboxZoomLevels.Append(str(resolution[0]),str(resolution))

    def setMetaData(self, tile_seed):
        self.setSelectedZoomLvl(tile_seed)
        self.setSelectedBB(tile_seed.tile_schema.extent)
    
    def setSelectedBB(self, extent):
        if not self.itemIsSelected == False:
            self.labelMinX.SetValue(str(extent.getMinX()))
            self.labelMinY.SetValue(str(extent.getMinY()))
            self.labelMaxX.SetValue(str(extent.getMaxX()))
            self.labelMaxY.SetValue(str(extent.getMaxY()))
            #self.__map.map_control.setTileSeedExtent(extent)
        